package com.zhouyibike.uu.ui.activity.mywallet;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.ConsumingYiQuanResult;
import com.zhouyibike.uu.entity.ProduceListResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.utils.DataSingle;
import com.zhouyibike.uu.utils.TextUtils;
import com.zhouyibike.uu.utils.Util;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

public class ShouhuorenxinxiActivity extends BaseActivity implements View.OnClickListener{
    TextView tv_zrnumber,tv_zrprice,shangpinname,shangpindanjia;
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    private int zrnum=1;
    private double shangpinprice=0;
    ProduceListResult.DataBean myshangpin;
    private ImageView tututu;
    private Button btn_gobuy;
    private EditText et_jvbao_note;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shouhuorenxinxi);
        zrnum=getIntent().getIntExtra("num",0);
        myshangpin= (ProduceListResult.DataBean) getIntent().getSerializableExtra("shangpin");
        shangpinprice=myshangpin.getPrice();
        initView();
        settixiannewDialog();
        btn_gobuy.setOnClickListener(this);
    }
    private void initView(){
        et_jvbao_note= (EditText) findViewById(R.id.et_jvbao_note);
        btn_gobuy= (Button) findViewById(R.id.btn_gobuy);
        tututu= (ImageView) findViewById(R.id.tututu);
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("收货人信息");
        mBtnBack.setOnClickListener(this);
        tv_zrnumber= (TextView) findViewById(R.id.goumainum);
        tv_zrprice=(TextView) findViewById(R.id.goumaiprice);
        shangpinname= (TextView) findViewById(R.id.shangpinname);
        shangpindanjia= (TextView) findViewById(R.id.shangpinprice);
        tv_zrnumber.setText("共计"+zrnum + "件");
        tv_zrprice.setText(myshangpin.getUnit()+df.format(Double.parseDouble(shangpinprice+"") * zrnum) );
        shangpinname.setText(myshangpin.getTitle());
        shangpindanjia.setText(myshangpin.getUnit()+df.format(Double.parseDouble(shangpinprice+"")));
        try {
            Util.setLviewwh(tututu,Double.parseDouble(myshangpin.getBannars().get(0).getWidth()),Double.parseDouble(myshangpin.getBannars().get(0).getHeight()),0.35);
            Picasso.with(this).load(myshangpin.getBannars().get(0).getImagePath()).placeholder(R.drawable.zhanweitu).error(R.drawable.zhanweitu)
                    .networkPolicy(NetworkPolicy.NO_CACHE,NetworkPolicy.NO_STORE) .into(tututu);  // 加载网络图片
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    DecimalFormat df = new DecimalFormat("######0");
    //提现对话框1
    Dialog jubaodialog;
    private void settixiannewDialog() {
        TextView tixiandialogquxiao, tixiandialogqueding, note1,note2;
        ImageView xuzhi;
        LayoutInflater inflater = LayoutInflater.from(ShouhuorenxinxiActivity.this);
        final View headview = inflater.inflate(R.layout.tixiantishi_dialog, null, false);
        tixiandialogquxiao = headview.findViewById(R.id.tixiandialogquxiao);
        tixiandialogqueding = headview.findViewById(R.id.tixiandialogqueding);
        note2 = headview.findViewById(R.id.note2);
        xuzhi= headview.findViewById(R.id.xuzhi);
        note1= headview.findViewById(R.id.notes);
//        note1.setVisibility(View.GONE);
        xuzhi.setVisibility(View.GONE);
        note2.setText("您即将购买本商品，请确认备注信息填写已完成，购买后备注信息将不可修改。如有问题请联系客服人员。");
        jubaodialog = new Dialog(this, R.style.Dialog_Fullscreen);
        jubaodialog.setContentView(headview, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        jubaodialog.setCanceledOnTouchOutside(false);
        Window dialogWindow = jubaodialog.getWindow();
        dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
//        adjustPan|stateHidden
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
//                lp.x = 100; // 新位置X坐标
//          lp.y = 100; // 新位置Y坐标
        lp.width = (int) (DataSingle.getInstance().PhoneW * 0.75); // 宽度
//        lp.height = (int) (DataSingle.getInstance().PhoneW * 0.75);
//         lp.alpha = 0.7f; // 透明度
        // 当Window的Attributes改变时系统会调用此函数,可以直接调用以应用上面对窗口参数的更改,也可以用setAttributes
        // dialog.onWindowAttributesChanged(lp);
        dialogWindow.setWindowAnimations(R.style.mypopwindow_anim_style3);  //添加动画
        dialogWindow.setAttributes(lp);
        tixiandialogquxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jubaodialog.dismiss();
            }
        });
        tixiandialogqueding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isNullorEmpty(et_jvbao_note.getText().toString().trim()))
                {
                    toastShow("备注信息不能为空");
                    jubaodialog.dismiss();
                }else{
                    getfukuanMsg();
                    jubaodialog.dismiss();
                }

            }
        });
        DialogInterface.OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                    return true;
                } else {
                    return false;
                }
            }
        };
        jubaodialog.setOnKeyListener(keylistener);
    }
    private void getfukuanMsg() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
        params.put("ProduceId",myshangpin.getproduceId()+"");
        params.put("ProCount", zrnum+"");
        params.put("Remarks", et_jvbao_note.getText().toString().trim());
//        showProgressDialog();
        addSubscription(apiStores.ConsumingYiQuan(params),
                new ApiCallback<ConsumingYiQuanResult>() {
                    @Override
                    public void onSuccess(ConsumingYiQuanResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                           Intent ii=new Intent(ShouhuorenxinxiActivity.this,BuyshangpinSuccessActivity.class);
                            ii.putExtra("time",myshangpin.getIntroduce());
                            startActivityForResult(ii,200);
                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);

                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==200&&resultCode==1)
        {
            setResult(101);
            finish();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_gobuy:
                jubaodialog.show();
                break;
            case R.id.btn_txsuccess_ljyc:
                finish();
                break;
            case R.id.btn_txsuccess_detail:
                Intent intent=new Intent(ShouhuorenxinxiActivity.this,MyDetailActivity.class);
                intent.putExtra("type","13");
                startActivity(intent);
                break;
        }
    }
}
